package com.hnx.admin.common.security.core;

import cn.hutool.core.util.StrUtil;
import com.hnx.admin.api.security.core.SecurityPropertyAPI;
import com.hnx.admin.common.redis.core.RedisCore;
import org.springframework.stereotype.Component;

import java.util.Optional;
import java.util.concurrent.TimeUnit;

/*
创建人： 倔强的头发
创建时间：2024/7/5
功能描述:

*/
@Component
public record VerifyError(RedisCore redisCache,SecurityProperties securityProperties) implements SecurityPropertyAPI {
    public String getRetryKey(String username) {
        return StrUtil.join("", PWD_ERR_CNT_KEY,Optional.ofNullable(username).orElse(""));
    }
    public Integer getRetryCount(String cacheKey) {
        return Integer.parseInt(Optional.ofNullable(redisCache.getCache(cacheKey)).orElse("0"));
    }
    public void clearLoginRecordCache(String loginName){
        if (StrUtil.isNotBlank(loginName) && redisCache.hasKey(loginName))
        {
            redisCache.deleteObject(loginName);
        }
    }
    public void setRetryCount(String cacheKey,Integer retryCount) {
        redisCache.setCacheObject(cacheKey, ++retryCount, securityProperties.getLockTime(), TimeUnit.MINUTES);
    }
}
